#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define ll long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(ll x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
bool _stmer;

ll lcm(ll n, ll m) { if (n == m) return 0; return n * m / __gcd(n, m); }

void solve() {
	ll n = read(), m = read();
	if (__gcd(n, m) > 1)
		return write(min(n + m, lcm(n, m))), (void)puts("");
	ll res1 = n, res2 = m;
	for (int i = 2; i * i <= n; i++)
		if (n % i == 0) { res1 = i; break; }
	for (int i = 2; i * i <= m; i++)
		if (m % i == 0) { res2 = i; break; }
	write(min(lcm(n, res1) + lcm(res1, 2) + lcm(2, res2) + lcm(res2, m), lcm(n, res1) + lcm(res2, m) + lcm(res1, res2))), puts("");
}

bool _edmer;
int main() {
	cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
#ifndef cxqghzj
	freopen("never.in", "r", stdin);
	freopen("never.out", "w", stdout);
#endif
	int T = read();
	while (T--) solve();
	return 0;
}
